package com.yangmao.service.impl;

import com.yangmao.dao.AdminDao;
import com.yangmao.entity.Admin;
import com.yangmao.service.api.AdminService;
import constant.SystemConstant;
import exception.LoginFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.SystemUtil;

import java.util.List;
import java.util.Objects;

/**
 * @author ‘洋毛’
 * @version 1.0
 * @description: TODO
 * @date 2021/12/11 11:22
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;

    @Override
    public List<Admin> getAllAdmin() {
        List<Admin> allAdmin = adminDao.getAllAdmin();

        return allAdmin;
    }

    @Override
    public Admin getAdminByAcct(String login_acct, String userPswd) {

        List<Admin> adminList = adminDao.getAdminByAcct(login_acct);

        // 2.判断Admin对象是否为空
        if (adminList == null || adminList.size() == 0) {
            throw new LoginFailedException(SystemConstant.MESSAGE_LOGIN_FAILED);
        }


        Admin admin = adminList.get(0);

        if(admin == null) {
            throw new LoginFailedException(SystemConstant.MESSAGE_LOGIN_FAILED);
        }

        String user_pswd = admin.getPassword();

        String userPswdForm = SystemUtil.md5(userPswd);

        if(!Objects.equals(userPswdForm,user_pswd)){
            throw new LoginFailedException(SystemConstant.MESSAGE_LOGIN_FAILED);
        }

        // 一致，返回用户
        return admin;

    }
}
